# _*_ coding: UTF-8 _*_
# @Time     : 2022/4/24 15:10
# @Author   : Li Jie
# @Site     : http://www.cdtest.cn/
# @File     : 等待机制.py
# @Software : PyCharm

"""
等待机制有三种：
1.强制等待。time.sleep(时间)。一般用于调试和演示，等待时间没有弹性
2.隐式等待。driver.implicitly_wait(时间)。
3.显式等待。灵活，默认每0.5秒判断等待条件是否成立，成立继续往下执行

from selenium.webdriver.support.wait import WebDriverWait   # 导入显式等待类
from selenium.webdriver.support import expected_conditions  # 导入等待条件

# 生成显式等待对象，设置最大元素等待时间
wait = WebDriverWait(driver, 30)

#在具体元素操作之前加等待语句
wait.until(expected_conditions.visibility_of_element_located((By.ID, "key")))
"""

from selenium import webdriver
import time
from selenium.webdriver.common.by import By


def foo1():
    """
    隐式等待
    :return:
    """
    driver = webdriver.Chrome()
    # 设置最大页面加载等待时间为30秒
    driver.implicitly_wait(30)

    driver.maximize_window()

    driver.get("https://www.jd.com/")

    driver.find_element(By.ID, "key").send_keys("T恤")  # 输入搜索输入框
    driver.find_element(By.XPATH, '//*[@id="search"]/div/div[2]/button').click()  # 点击搜索按键

    driver.find_element(By.XPATH, '//*[@id="J_filter"]/div[1]/div[1]/a[2]').click()  # 点击销量

    # 循环打开销量前5
    for i in range(1, 6):
        driver.find_element(By.XPATH, f'//*[@id="J_goodsList"]/ul/li[{i}]/div/div[1]/a').click()

    # 切换到销量第二的产品
    handles = driver.window_handles
    for handle in handles:
        driver.switch_to.window(handle)
        if driver.current_url == "https://item.jd.com/10030434646509.html":
            break

    # 选择产品参数
    driver.find_element(By.XPATH, '//*[@id="choose-attr-1"]/div[2]/div[4]').click()  # 选款式

    for i in range(4):
        driver.find_element(By.XPATH, '//*[@id="choose-btns"]/div/div/a[2]').click()
    driver.find_element(By.XPATH, '//*[@id="choose-attr-2"]/div[2]/div[3]').click()  # 选尺码

    # 点击购物车
    driver.find_element(By.ID, 'InitCartUrl').click()

    # 点击账号登录
    driver.find_element(By.LINK_TEXT, '账户登录').click()

    # 输入账号和密码，点击登录
    driver.find_element(By.ID, 'loginname').send_keys("test")
    driver.find_element(By.ID, 'nloginpwd').send_keys('123456')
    driver.find_element(By.ID, 'loginsubmit').click()

    driver.quit()


from selenium.webdriver.support.wait import WebDriverWait   # 导入显式等待类
from selenium.webdriver.support import expected_conditions  # 导入等待条件


def foo2():
    """
    显式等待
    :return:
    """
    driver = webdriver.Chrome()
    # 生成显式等待对象，设置最大元素等待时间
    wait = WebDriverWait(driver, 30)

    driver.maximize_window()

    driver.get("https://www.jd.com/")

    #加入显式等待
    wait.until(expected_conditions.visibility_of_element_located((By.ID, "key")))
    # wait.until(expected_conditions.presence_of_element_located((By.ID, "key")))
    driver.find_element(By.ID, "key").send_keys("T恤")  # 输入搜索输入框
    driver.find_element(By.XPATH, '//*[@id="search"]/div/div[2]/button').click()  # 点击搜索按键

    wait.until(expected_conditions.visibility_of_element_located((By.XPATH, '//*[@id="J_filter"]/div[1]/div[1]/a[2]')))
    driver.find_element(By.XPATH, '//*[@id="J_filter"]/div[1]/div[1]/a[2]').click()  # 点击销量

    # 循环打开销量前5
    for i in range(1, 6):
        wait.until(expected_conditions.visibility_of_element_located((By.XPATH, f'//*[@id="J_goodsList"]/ul/li[{i}]/div/div[1]/a')))
        driver.find_element(By.XPATH, f'//*[@id="J_goodsList"]/ul/li[{i}]/div/div[1]/a').click()

    # 切换到销量第二的产品
    handles = driver.window_handles
    for handle in handles:
        driver.switch_to.window(handle)
        if driver.current_url == "https://item.jd.com/10030434646509.html":
            break

    # 选择产品参数
    wait.until(expected_conditions.visibility_of_element_located((By.XPATH, '//*[@id="choose-attr-1"]/div[2]/div[4]')))
    driver.find_element(By.XPATH, '//*[@id="choose-attr-1"]/div[2]/div[4]').click()  # 选款式

    for i in range(4):
        driver.find_element(By.XPATH, '//*[@id="choose-btns"]/div/div/a[2]').click()
    driver.find_element(By.XPATH, '//*[@id="choose-attr-2"]/div[2]/div[3]').click()  # 选尺码

    # 点击购物车
    driver.find_element(By.ID, 'InitCartUrl').click()

    # 点击账号登录
    wait.until(expected_conditions.visibility_of_element_located((By.LINK_TEXT, '账户登录')))
    driver.find_element(By.LINK_TEXT, '账户登录').click()

    # 输入账号和密码，点击登录
    wait.until(expected_conditions.visibility_of_element_located((By.ID, 'loginname')))
    driver.find_element(By.ID, 'loginname').send_keys("test")
    driver.find_element(By.ID, 'nloginpwd').send_keys('123456')
    driver.find_element(By.ID, 'loginsubmit').click()

    driver.quit()


if __name__ == "__main__":
    # foo1()
    foo2()